import 'package:inmotion_pro/http/api_helper.dart';
import 'package:inmotion_pro/pages/models/http_response/http_response_model.dart';
import 'package:lx_widgets_library/lx_widgets_library.dart';
import '../custom_exception/api_exception.dart';

// 售后相关的API扩展
extension ApiHelperAfterSaleExtension on ApiHelper {
  // MARK: - 新增工单
  Future<String> createWorkOrder(
    String sn,
    String userName,
    String phone,
    int repairType,
    String content,
    List<String> attachment,
    Map<String, dynamic>? diagnosis,
  ) {
    return httpManager.request<HttpResponseModel>(
      RequestType.POST,
      "/v2/after-sales/ticket/new",
      HttpResponseModel.fromJson,
      data: {
        "sn": sn,
        "user_name": userName,
        "phone": phone,
        "repair_type": repairType,
        "content": content,
        "attachment": attachment,
        if (diagnosis != null) "diagnosis": diagnosis,
      },
    ).then((response) {
      if (!response.isSuccess) {
        throw ApiException(response.code, response.message);
      }
      return response.originalResponse.data['data'];
    });
  }

  // MARK: - 更新工单
  Future<bool> updateWorkOrder(
    String orderId,
    List<String> feedbackAttachment, {
    String? feedbackContent,
  }) {
    return httpManager.request<HttpResponseModel>(
      RequestType.POST,
      "/v2/after-sales/ticket/update",
      HttpResponseModel.fromJson,
      data: {
        "id": orderId,
        "feedback_attachment": feedbackAttachment,
        if (feedbackContent != null) "feedback_content": feedbackContent,
      },
    ).then((response) {
      if (!response.isSuccess) {
        throw ApiException(response.code, response.message);
      }
      return response.isSuccess;
    });
  }

  // MARK: - 工单详情
  Future<Map<String, dynamic>> getWorkOrderDetail(String orderId) {
    return httpManager.request<HttpResponseModel>(
      RequestType.GET,
      "/v2/after-sales/ticket/detail",
      HttpResponseModel.fromJson,
      queryParameters: {
        "id": orderId,
        ...baseGetMap(),
      },
    ).then((response) {
      if (!response.isSuccess || response.data == null) {
        throw ApiException(response.code, response.message);
      }
      return response.data!.originData;
    });
  }

  // MARK: - 工单列表
  Future<Map<String, dynamic>> getWorkOrderList({
    int? page,
    int? pageSize,
  }) {
    return httpManager.request<HttpResponseModel>(
      RequestType.GET,
      "/v2/after-sales/ticket/list",
      HttpResponseModel.fromJson,
      queryParameters: {
        if (page != null) "page": page,
        if (pageSize != null) "page_size": pageSize,
        ...baseGetMap(),
      },
    ).then((response) {
      if (!response.isSuccess || response.data == null) {
        throw ApiException(response.code, response.message);
      }
      return response.data!.originData;
    });
  }

  // MARK: - 工单已解决
  Future<bool> markWorkOrderSolved(String orderId) {
    return httpManager.request<HttpResponseModel>(
      RequestType.POST,
      "/v2/after-sales/result/update",
      HttpResponseModel.fromJson,
      data: {
        "id": orderId,
        "status": 1,
      },
    ).then((response) {
      if (!response.isSuccess) {
        throw ApiException(response.code, response.message);
      }
      return response.isSuccess;
    });
  }

  // MARK: - 工单未解决
  Future<bool> markWorkOrderUnsolved(
    String orderId,
    String comment,
    List<String>? attachment,
  ) {
    return httpManager.request<HttpResponseModel>(
      RequestType.POST,
      "/v2/after-sales/result/update",
      HttpResponseModel.fromJson,
      data: {
        "id": orderId,
        "status": -1,
        "comment": comment,
        if (attachment != null) "attachment": attachment,
      },
    ).then((response) {
      if (!response.isSuccess) {
        throw ApiException(response.code, response.message);
      }
      return response.isSuccess;
    });
  }
}
